package org.tpdata.api.singlequality.hospitalqc;

import asserts.Asserts;
import business.inhospitalqc.HistoryList;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import com.google.common.base.Objects;
import common.CaseInfo;
import common.Data;
import common.RunCaseJson;
import common.SetUpTearDown;
import exceptions.TestNGRetry;
import io.qameta.allure.Feature;
import io.qameta.allure.Story;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.Contants;
import utils.ExcelUtils;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import static business.inhospitalqc.LaunchQc.timestamp;
import static common.TestStep.requestAndRespondBody;

@Feature("获取单个患者质控历史的reportdatas数据")
public class GetPersonalReport extends SetUpTearDown {
    Logger log = Logger.getLogger(GetPersonalReport.class);
    Data data = envSetUp();
    HistoryList historyList=new HistoryList();
//    String str=historyList.GetHistoryList("COPD-001","1","api-sourcedata/qc/batch/patient/list.json");
String str;

    {
        try {
            str = historyList.GetHistoryList(0);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (YamlException e) {
            e.printStackTrace();
        }
    }

    JSONObject jsonObject= JSON.parseObject(str);
    JSONObject obj=jsonObject.getJSONObject("responseData");
    JSONArray jsonArray=obj.getJSONArray("content");
    JSONObject object= jsonArray.getJSONObject(0);
    String inpatient_no=object.getString("inpatient_no");

    @Story("在实时质控历史页获取第一个患者的详情页")
    @BeforeTest


    @Test(retryAnalyzer = TestNGRetry.class,description = "获取列表", dataProvider = "datas")
    public void runCase(CaseInfo caseInfo) {
        JSONObject json = new JSONObject();
//        Map<String,String > json=new HashMap<String, String>();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("reportNos", caseInfo.getReportNos());     //  报告号，多个值以逗号连接  值01002
        //nzkt0018 实时质控评价次数
        //nzkt0019 回顾质控评价次数
        //nzkt0020 住院天数
        //nzkt0021 住院总费用
        //nzkt0022 治疗变化表
        //nzkt0031  当前病例住院期间PaO2、PaCO2、pH值的变化趋势
        json.put("indexTimeStart", caseInfo.getIndexTimeStart());
        json.put("indexTimeEnd", caseInfo.getIndexTimeEnd());
        json.put("inpatient_no", inpatient_no);
        json.put("medCode", caseInfo.getMedCode());
        json.put("timeStamp", timestamp);
//        json.put("", "");
        log.info(json.toString());
        Response response = RunCaseJson.runCase(json, "get", caseInfo.getUri());
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);

        JSONObject obj = JSONObject.parseObject(jsonStr);
        JSONArray contentArr = obj.getJSONArray("responseData");
        String url = RestAssured.baseURI + ":" + RestAssured.port + caseInfo.getUri();

        requestAndRespondBody(url, json, jsonStr);
        boolean flag = Asserts.VerityPersonalReport(contentArr);
        Assert.assertEquals(flag, Contants.SUCCESS);
    }

    @DataProvider
    public Object[] datas() {
        List<CaseInfo> list = ExcelUtils.read(this.sheetIndex, 1, CaseInfo.class);
        List lt = new ArrayList();
        String StrName=this.getClass().getSimpleName();
        for (CaseInfo caseInfo : list) {
            if (Objects.equal(caseInfo.getClassName(),StrName) ) {
                lt.add(caseInfo);
            }
        }
//        return list.toArray();
        return lt.toArray();

    }

}
